package com.jiepos.mpos.biz.service.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jiepos.mpos.biz.service.service.CustomServiceParamStlService;
import com.jiepos.mpos.biz.service.service.ServiceParamService;
import com.jiepos.mpos.biz.user.service.AppInsInfService;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.entity.App_Custom_Service_Param_StlEntity;
import com.jiepos.mpos.common.entity.App_Ins_InfEntity;
import com.jiepos.mpos.common.entity.App_Service_InfEntity;
import com.jiepos.mpos.common.entity.App_Service_ParamEntity;
import com.jiepos.mpos.core.controller.BaseController;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 会员业务参数结算维护Controller
 * @author Zhao.Jinkui
 * @version 1.0
 * @ProjectName: mpos
 * @Package com.jiepos.mpos.biz.service.controller
 * @ClassName CustomServiceParamStlController
 * @history
 * @UpdateRemark []
 * @since 2017/9/29
 */
@RequestMapping("customServiceParamStl")
@Controller
public class CustomServiceParamStlController extends BaseController{

    @Autowired
    private CustomServiceParamStlService customServiceParamStlService;

    @Autowired
    private ServiceParamService serviceParamService;

    @Autowired
    private AppInsInfService appInsInfService;

    /**
     * 进入会员业务参数结算页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getCustomServiceParamStlList.do")
    public ModelAndView getCustomServiceParamStlList(HttpServletRequest request, HttpServletResponse response){
        ModelAndView model = new ModelAndView("/module/service/customserviceparamstl/customserviceparamstl_list");
        return super.getDetail(request, response, model);
    }

    /**
     * 查询会员业务参数结算Json数据
     * @param pagination
     * @param appServiceInfEntity
     * @param appInsInfEntity
     * @return
     */
    @RequestMapping("/getCustomServiceParamStlListJson.do")
    public @ResponseBody
    Map getCustomServiceParamStlListJson(HttpServletRequest request,Pagination pagination, App_Service_InfEntity appServiceInfEntity, App_Ins_InfEntity appInsInfEntity){
        return customServiceParamStlService.findByCondition(request,pagination,appServiceInfEntity,appInsInfEntity);
    }

    /**
     * 进入新增会员业务参数结算界面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/getCustomServiceParamStlAdd.do")
    public ModelAndView getCustomServiceParamStlAdd(HttpServletRequest request, HttpServletResponse response){
        ModelAndView model = new ModelAndView("/module/service/customserviceparamstl/customserviceparamstl_detail");
        return super.getDetail(request, response, model);
    }

    /**
     * 获取业务树(编辑)
     * @param ins_uuid 机构uuid
     * @return
     */
    @RequestMapping("/getServiceInfTreeEdit.do")
    @ResponseBody
    public List<Map<String, Object>> getServiceInfTreeEdit(String ins_uuid){
        return customServiceParamStlService.getServiceInfTreeEdit(ins_uuid);
    }


    /**
     * 保存业务参数结算信息
     *
     * @param request
     * @param response
     * @param appCustomServiceParamStlEntity
     * @return
     */
    @RequestMapping("/saveCustomServiceParamStl.do")
    @ResponseBody
    public Map<String,String> saveCustomServiceParamStl(HttpServletRequest request, HttpServletResponse response, App_Custom_Service_Param_StlEntity appCustomServiceParamStlEntity) {
        Map<String,String> result = new HashMap<>();
        if (!StringUtils.isBlank(appCustomServiceParamStlEntity.getIns_uuid()) && !StringUtils.isBlank(appCustomServiceParamStlEntity.getService_uuid())
                && !StringUtils.isBlank(appCustomServiceParamStlEntity.getService_param_uuid())) {
            //获取该 系统业务参数信息
            App_Service_ParamEntity serviceParam = serviceParamService.findByUUID(appCustomServiceParamStlEntity.getService_param_uuid());
            //进行校验（会员扣率等相关值不能低于系统业务参数设置的值）
            List<String> list = customServiceParamStlService.checkInsParamStl(serviceParam, appCustomServiceParamStlEntity);
            if(list.size() > 0){
                //不能保存
                StringBuffer sb = new StringBuffer();
                sb.append(list.toString());
                sb.append("低于系统设定值，请修改");
                result.put("code",SystemConstant.ERROR);
                result.put("message",sb.toString());
            }else{
                //可以保存
                String saveResult = customServiceParamStlService.saveCustomServiceParamStl(super.getLoginUser(request, response),appCustomServiceParamStlEntity);
                if(SystemConstant.SUCCESS.equals(saveResult)){
                    result.put("code",SystemConstant.SUCCESS);
                    result.put("message","保存成功！");
                }else{
                    result.put("code",SystemConstant.ERROR);
                    result.put("message","保存失败!");
                }
            }

        } else {
            result.put("code",SystemConstant.ERROR);
            result.put("message","请点击左侧业务参数！");
        }
        return result;
    }

    /**
     * 加载会员参数结算设置数据
     * @param ins_uuid
     * @param service_uuid
     * @param service_param_uuid
     * @return
     */
    @RequestMapping("/getCustomServiceParamStlByUUID.do")
    @ResponseBody
    public ModelAndView getCustomServiceParamStlByUUID(String ins_uuid,String service_uuid,String service_param_uuid,String menuCode){
        ModelAndView model = new ModelAndView("/module/service/customserviceparamstl/customserviceparamstl_edit");
        model.addObject("menuCode",menuCode);
        if(!StringUtils.isBlank(ins_uuid) && !StringUtils.isBlank(service_uuid) && !StringUtils.isBlank(service_param_uuid)){
            App_Custom_Service_Param_StlEntity appCustomServiceParamStlEntity = customServiceParamStlService.getCustomServiceParamStlDataByUUID(ins_uuid, service_uuid, service_param_uuid);
            model.addObject("appCustomServiceParamStlEntity",appCustomServiceParamStlEntity);
            model.addObject("ins_uuid",ins_uuid);
            model.addObject("service_uuid",service_uuid);
            model.addObject("service_param_uuid",service_param_uuid);
        }
        return model;
    }

    /**
     * 更新会员参数结算设置树
     * @param request
     * @param response
     * @param ins_uuid
     * @param service_param_uuids
     * @return
     */
    @RequestMapping("/updateCustomParamStlSetTree.do")
    @ResponseBody
    public String updateCustomParamStlSetTree(HttpServletRequest request, HttpServletResponse response,String ins_uuid,String service_param_uuids){
        return customServiceParamStlService.updateCustomParamStlSetTree(ins_uuid,service_param_uuids,super.getLoginUser(request, response));
    }

    /**
     * 进入修改会员业务参数结算页面
     * @param request
     * @param response
     * @param ins_uuid
     * @return
     */
    @RequestMapping("/getCustomServiceParamStlDetail.do")
    @ResponseBody
    public ModelAndView getCustomServiceParamStlDetail(HttpServletRequest request, HttpServletResponse response,String ins_uuid){
        ModelAndView model = new ModelAndView("/module/service/customserviceparamstl/customserviceparamstl_detail");
        //根据机构id加载机构信息 "INS_CD" "INS_CN_NM"
        if(!StringUtils.isBlank(ins_uuid)){
            String[] split = ins_uuid.split(":");
            ins_uuid = split[0];
        }
        Map appInsInfEntity=appInsInfService.getInsInfById(request,ins_uuid);
        model.addObject("ins_uuid",ins_uuid);
        model.addObject("inst_name",appInsInfEntity.get("INS_CD")+"-"+appInsInfEntity.get("INS_CN_NM"));
        return super.getDetail(request, response, model);
    }
}
